﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;
using System.Data.SqlClient;

namespace Warehouse.Web.Management
{
    public partial class Suppliers : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected override void ProcessAjaxRequest(HttpContext context, string action)
        {
            switch (action)
            {
                case "Get":
                    {
                        int CarrierID = int.Parse(context.Request.QueryString["SupplierID"]);
                        var supplier = DataAccess.Supplier.GetSupplier(CarrierID);
                        if (supplier != null)
                        {
                            WebUtil.WriteJsonResult(context, supplier);
                        }
                        else
                        {
                            WebUtil.WriteJsonError(context, "供货商已经被删除");
                        }
                    }
                    break;
                case "Add":
                    {
                        string Name = context.Request.Form["Name"].Trim();
                        string Code = context.Request.Form["Code"].Trim();
                        string Address = context.Request.Form["Address"];
                        string Telephone = context.Request.Form["Telephone"];
                        if (string.IsNullOrEmpty(Name))
                        {
                            WebUtil.WriteJsonError(context, "名称不能为空");
                            return;
                        }
                        try
                        {
                            var supplier = new DataAccess.Supplier()
                            {
                                Name = Name,
                                Code = Code,
                                Address = Address,
                                Telephone = Telephone
                            };
                            supplier.Save();
                            WebUtil.WriteJsonResult(context, null);
                        }
                        catch (SqlException ex)
                        {
                            if (ex.Number == 2601)
                            {
                                WebUtil.WriteJsonError(context, "该名称已存在，请更改名称后再保存");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    break;
                case "Save":
                    {
                        int SupplierID = int.Parse(context.Request.Form["ID"]);
                        string Name = context.Request.Form["Name"].Trim();
                        string Code = context.Request.Form["Code"].Trim();
                        string Address = context.Request.Form["Address"];
                        string Telephone = context.Request.Form["Telephone"];
                        if (string.IsNullOrEmpty(Name))
                        {
                            WebUtil.WriteJsonError(context, "名称不能为空");
                            return;
                        }
                        try
                        {
                            var supplier = DataAccess.Supplier.GetSupplier(SupplierID);
                            if (supplier != null)
                            {
                                supplier.Name = Name;
                                supplier.Code = Code;
                                supplier.Address = Address;
                                supplier.Telephone = Telephone;

                                supplier.Save();
                                WebUtil.WriteJsonResult(context, null);
                            }
                            else
                            {
                                WebUtil.WriteJsonError(context, "供货商已经被删除");
                            }
                        }
                        catch (SqlException ex)
                        {
                            if (ex.Number == 2601)
                            {
                                WebUtil.WriteJsonError(context, "该名称已存在，请更改名称后再保存");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    break;
                case "Delete":
                    {
                        int SupplierID = int.Parse(context.Request.Form["SupplierID"]);
                        var supplier = DataAccess.Supplier.GetSupplier(SupplierID);
                        if (supplier != null)
                        {
                            try
                            {
                                supplier.Delete();
                                WebUtil.WriteJsonResult(context, null);
                            }
                            catch (SqlException ex)
                            {
                                if (ex.Number == 547)
                                {
                                    WebUtil.WriteJsonError(context, "该供货商已有供货记录，不能删除");
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            WebUtil.WriteJsonResult(context, null);
                        }
                    }
                    break;
                default:
                    base.ProcessAjaxRequest(context, action);
                    break;
            }
        }

        void LoadData()
        {
            this.rptItems.DataSource = DataAccess.Supplier.GetSuppliers();
            this.rptItems.DataBind();
        }
    }
}
